package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.FeedReportDao;
import com.jiantong.work.impl.manager.FeedReportManager;
import com.jiantong.work.model.FeedReport;
import com.jiantong.work.service.FeedReportWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 */
@Slf4j
@Service
@RpcProvider
public class FeedReportWriteServiceImpl implements FeedReportWriteService {
    private final FeedReportDao feedReportDao;
    private final FeedReportManager feedReportManager;

    public FeedReportWriteServiceImpl(FeedReportDao feedReportDao, FeedReportManager feedReportManager) {
        this.feedReportDao = feedReportDao;
        this.feedReportManager = feedReportManager;
    }

    /**
     * 创建饲料中心新单据
     * @param feedReport 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Map<String,Object>> create(FeedReport feedReport,Integer toStatus) {
        try {
            return Response.ok(feedReportManager.createFeedReport(feedReport,toStatus));
        }catch (Exception e){
            log.error("failed.to.create {},case:{}" ,feedReport,Throwables.getStackTraceAsString(e));
            return Response.fail("feedReport.report.create.fail");
        }
    }

    /**
     *
     * @param feedReport 单据对象
     * @return
     */
    @Override
    public Response<Map<String,Object>> update(FeedReport feedReport,Integer toStatus) {
        try {
            return Response.ok(feedReportManager.updateProductionReport(feedReport,toStatus));
        }catch (Exception e){
            log.error("failed.to.update {},case:{}" ,feedReport,Throwables.getStackTraceAsString(e));
            return Response.fail("feed.report.update.fail");
        }
    }

    @Override
    public Boolean update(FeedReport feedReport) {
        return feedReportDao.update(feedReport);
    }
}
